package com.markhsiu.framework.rpc.test;

import java.util.concurrent.CountDownLatch;

import org.apache.commons.lang3.time.StopWatch;

import com.markhsiu.framework.rpc.client.MessageSendExecutor;

/**
 * rpc并发测试代码
 * @author Mark Hsiu
 *
 */
public class ClientRpc {
	
	//并行度10000
	private  static final int PARALLEL = 10000;
	private  static final String IP_PORT = "127.0.0.1:18888";
	
    public static void main(String[] args) throws Exception {
        final MessageSendExecutor executor = new MessageSendExecutor(IP_PORT);
       
        

        CountDownLatch signal = new CountDownLatch(1);
        CountDownLatch finish = new CountDownLatch(PARALLEL);

        for (int index = 0; index < PARALLEL; index++) {
            CalcParallelRequestThread client = new CalcParallelRequestThread(executor, signal, finish, index);
            new Thread(client).start();
        }
        
        //PARALLEL个并发线程瞬间发起请求操作
        signal.countDown();
        
        //开始计时
        StopWatch sw = new StopWatch();
        sw.start();
        finish.await();
        sw.stop();

        String tip = String.format("RPC调用总共耗时: [%s] 毫秒", sw.getTime());
        System.out.println(tip);

        executor.stop();
    }
}